import { IHeapSnapshot, utils, AnyOptions } from "@memlab/core";
import { getFullHeapFromFile } from "@memlab/heap-analysis";
import { Log } from "../utils/Log";

/**
 * Loader
 */
export class Loader {
    public static readonly DOMAIN = 'HeapSnapshot';
    public static readonly TAG = 'Loader';

    /**
     * 加载HeapSnapshot文件
     * @param file 文件路径
     * @param log 是否打印日志
     * @returns IHeapSnapshot 对象
     */
    public static async loadFromFile(file: string, log: boolean = false) : Promise<IHeapSnapshot> {
        if (log) Log.info(Loader.DOMAIN, Loader.TAG, `Loading heapsnapshot from file: ${file}`);
        const snapshot = await getFullHeapFromFile(file);
        if (log) Log.info(Loader.DOMAIN, Loader.TAG, `Loaded heapsnapshot from file: ${file}`);         
        return snapshot;
    }

    /**
     * 加载HeapSnapshot文件
     * @param file 文件路径
     * @param options 可选参数选项
     * @param log 是否打印日志
     * @returns IHeapSnapshot 对象
     */
    public static async loadFromFileOption(file: string, 
        options: AnyOptions, log: boolean = false) : Promise<IHeapSnapshot> {
        if (log) Log.info(Loader.DOMAIN, Loader.TAG, `Loading heapsnapshot from file: ${file}`);         
        const snapshot = await utils.getSnapshotFromFile(file, options);
        if (log) Log.info(Loader.DOMAIN, Loader.TAG, `Loaded heapsnapshot from file: ${file}`);
        return snapshot
    }

    /**
     * Heapsnapshot 多文件加载
     * @param files 多文件路径集合
     * @returns IHeapSnapshot 对象集合
     */
    public static async loadFromFiles(files: Array<string>) : Promise<Array<IHeapSnapshot>> {
        const result = new Array<IHeapSnapshot>();
        files.forEach(async (file) => {
            const heap = await getFullHeapFromFile(file);
            result.push(heap);
        });
        return result;
    }
}